package org.dev.lms.controller;

import org.dev.lms.service.BookCatalogService;
import org.dev.lms.entity.BookCatalog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dev.common.core.page.PaginAtion;
import org.dev.common.core.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.dev.common.core.aop.OperLog;

import java.util.List;

@RestController
@RequestMapping("/bookCatalog")
public class BookCatalogController {
    @Autowired
    public BookCatalogService bookCatalogService;

    /**
     * 保存和修改公用的
     *
     * @param bookCatalog 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperLog(description = "bookCatalog保存和修改")
    public ResponseResult<String> save(@RequestBody BookCatalog bookCatalog) {
        boolean result = false;
        try {
            if (bookCatalog.getId() != null) {
                result = bookCatalogService.updateById(bookCatalog);
            } else {
                result = bookCatalogService.save(bookCatalog);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }

    /**
     * 获取对象信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @OperLog(description = "bookCatalog获取对象信息")
    public ResponseResult<BookCatalog> get(@RequestParam("id") Long id) {
        BookCatalog bookCatalog = bookCatalogService.getById(id);
        return ResponseResult.success(bookCatalog);
    }

    /**
     * 删除对象信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @OperLog(description = "bookCatalog删除对象信息")
    public ResponseResult<String> delete(@RequestBody List<Long> ids) {
        try {
            bookCatalogService.removeByIds(ids);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 分页查询数据信息
     *
     * @param bookCatalog
     * @param pagination
     * @return
     */
    @RequestMapping("/list")
    @OperLog(description = "bookCatalog分页查询数据信息")
    public ResponseResult<IPage<BookCatalog>> list(BookCatalog bookCatalog, PaginAtion pagination) {
        Page page = pagination.getPage();
        return ResponseResult.success(bookCatalogService.page(page, new QueryWrapper<BookCatalog>(bookCatalog)));
    }

    @RequestMapping("/all-list")
    @OperLog(description = "bookCatalog全部信息")
    public ResponseResult<List<BookCatalog>> all(BookCatalog bookCatalog) {
        return ResponseResult.success(bookCatalogService.list(new QueryWrapper<BookCatalog>(bookCatalog)));
    }
}